package com.cuiweiyou.happypaint.util;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

public class SoundUtil {
    private static SoundUtil instance;
    private final SoundPool soundPool;

    static {
        instance = new SoundUtil();
    }

    private SoundUtil() {
        AudioAttributes attributes = new AudioAttributes.Builder()//Call requires API level 21
                .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION) //
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) //
                .build();

        soundPool = new SoundPool.Builder() //
                .setMaxStreams(2) //
                .setAudioAttributes(attributes) //
                .build();
        Log.e("ard", "SoundUtil");
    }


    public static SoundUtil getInstance() {
        return instance;
    }

    public void play(Context context, int rawId) {
        int soundId = soundPool.load(context, rawId, 1);
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                soundPool.play(soundId, 0.5f, 0.5f, 0, 0, 1);
            }
        });
    }
}
